//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.persistence;

import java.io.PrintStream;
import java.sql.*;
import org.jpgolay.wi.propertie.PropertyCRUD;

public class ConnectDB {

    public ConnectDB() {
        PropertyCRUD prop = new PropertyCRUD();
        motDePasse = prop.getPass();
        nomUtilisateur = prop.getUser();
        url = prop.getUrl();
        try {
            Class.forName(prop.getDriver());
        } catch (ClassNotFoundException cnfex) {
            System.out.println("Impossible de charger le driver JDBC !\ndriver : " + prop.getDriver());
            System.out.println(cnfex);
        }
        open();
    }

    protected static void open() {
        try {
            connection = DriverManager.getConnection(url, nomUtilisateur, motDePasse);
        } catch (SQLException sqlex) {
            System.out.println("Impossible d'ouvrir une connection sur la base de donn\351e !\nurl : " + url);
            System.out.println(sqlex);
        }
    }

    public static Connection getConnection() {
        try {
            if (connection.isClosed()) {
                open();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            open();
        }
        return connection;
    }

    protected static Connection getNewConnexion() {
        try {
            return DriverManager.getConnection(url, nomUtilisateur, motDePasse);
        } catch (SQLException sqlex) {
            System.out.println("Impossible d'ouvrir une connection sur la base de donn\351e !\nurl : " + url);
            System.out.println(sqlex);
            return null;
        }
    }

    public boolean isConnected() {
        return connection != null;
    }

    public static void closeConnection(Connection c) {
        try {
            c.close();
        } catch (SQLException sqlex) {
            System.out.println("Impossible de fermer la connection sur la base de donn\351e !\nurl : " + url);
            System.out.println(sqlex);
        }
    }

    public void close() {
        try {
            connection.close();
        } catch (SQLException sqlex) {
            System.out.println("Impossible de fermer la connection sur la base de donn\351e !\nurl : " + url);
            System.out.println(sqlex);
        }
    }
    private static Connection connection;
    private static String url;
    private static String nomUtilisateur;
    private static String motDePasse;
}
